import type { Router } from 'vue-router';

import NProgress from 'nprogress';
import { useAppStore } from '@/settings/stores';

export default function setupCommunityInfoGuard(router: Router) {
  router.beforeEach(async () => {
    NProgress.start();

    const appStore = useAppStore();

    if (appStore.config.name) {
      return true;
    }
    else {
      const { isSuccess } = await appStore.fetchConfig();

      if (!isSuccess) {
        NProgress.done();

        // 取不出来社区基本信息，无法进行，只能跳转 404
        location.replace('/404');

        return false;
      }

      return isSuccess;
    }
  });
}
